<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="format-detection" content="telephone=no">
	<meta name="msapplication-tap-highlight" content="no">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="color-scheme" content="light dark">
	<title>欢迎</title>
	<link rel="stylesheet" href="https://cdn.staticfile.org/pure/2.0.5/pure-min.min.css">
	<style>
		.green {
			background-color: green;
		}

		div {
			margin-top: 45vh;
			text-align: center;
			padding: 10px;
		}
	</style>
</head>

<body>
	<div>
		<button class="pure-button pure-button-primary green" id="btn">微信授权登录</button>
		<button class="pure-button pure-button-primary" id="btn1">下载app</button>
		<br>
		<a href="reg.html">注册</a>
		<br>
		<a href="login.html">登录</a>

	</div>

	<script src="/scriptZIP/qs.min.js"></script>
	<script src="/scriptZIP/axios.min.js"></script>
	<script src="cordova.js"></script>

	<script>

		sessionStorage.app = 1;
		var url = Qs.parse(location.search, { ignoreQueryPrefix: true }).url, o;
		if (url) {
			if (url.indexOf('redirect_uri') >= 0) {
				url = url.split('#')[0];
				o = Qs.parse(url.split('?')[1]);
				url = o.redirect_uri + '?' + Qs.stringify({ code: 'app', state: o.state });
			}
			sessionStorage.url = url;
		}

		document.addEventListener('deviceready', function () {

			axios.get('/app_version.json').then(function (res) {
				var o = res.data;
				util.getVersion(function (ver) {

				});
			});


			function check() {
				axios.get('/api/wechat/session').then(function (res) {
					var o = res.data;
					if (!o.account) return;
					util.ws(o.account.openid);
					location.replace((o.teacher || o.student) ? 'index2.html' : '/wechat/index.html');
				});
			}

			check();

			document.getElementById('btn').onclick = function () {
				util.login(function (code) {
					var params = new URLSearchParams();
					params.append('code', code);
					axios.post('/api/wechat/app', params).then(function (res) {
						check();
					});
				});
			};

			document.getElementById('btn1').onclick = function () {
				util.download('https://wei.hxt5.com/app-release.apk');
			};

		}, false);

	</script>
</body>

</html>